package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.service.UserService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description UserController
 * @Author luohai
 * @Date 2024-10-24
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户信息模块")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /***
     *  多条件查询用户分页列表
     * @param userDto 用户DTO查询条件
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return: Page<UserVo>
     */
    @PostMapping("page/{pageNum}/{pageSize}")
    @ApiOperation(value = "用户分页",notes = "用户分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userDto",value = "用户DTO对象",required = true,dataType = "UserDto"),
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer")
    })
    @ApiOperationSupport(includeParameters = {"userDto.username","userDto.email","userDto.dataState","userDto.deptNo"})
    public ResponseResult<PageResponse<UserVo>> findUserVoPage(
            @RequestBody UserDto userDto,
            @PathVariable("pageNum") int pageNum,
            @PathVariable("pageSize") int pageSize) {
        PageResponse<UserVo> userVoPage = userService.findUserPage(userDto, pageNum, pageSize);

        String subject = UserThreadLocal.getSubject();
        System.out.println(subject);

        return ResponseResult.success(userVoPage);
    }

    /**
     * 用户添加
     * @param userDto
     * @return
     */
    @PutMapping
    @ApiOperation("用户添加")
    public ResponseResult userAddition(@RequestBody UserDto userDto){
        userService.userAddition(userDto);
        return ResponseResult.success();
    }

    /**
     * 用户修改
     * @param userDto
     * @return
     */
    @PatchMapping
    public ResponseResult userModification(@RequestBody UserDto userDto){
        userService.userModification(userDto);
        return ResponseResult.success();
    }

    /**
     * 启用或禁用用户
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation("启用或禁用用户")
    public ResponseResult enableDisable(@ApiParam("用户id") @PathVariable Long id,@ApiParam("用户状态") @PathVariable String status){
        userService.enableDisable(id,status);
        return ResponseResult.success();
    }

    /**
     * 删除用户
     * @param userIds
     * @return
     */
    @DeleteMapping("/remove/{userIds}")
    @ApiOperation("删除用户")
    public ResponseResult deleteUser(@ApiParam("用户id") @PathVariable String userIds){
        userService.deleteUser(userIds);
        return ResponseResult.success();
    }
}
